package com.kehutong.contact.admin;

import com.kehutong.contact.entity.ResidentCar;
import com.kehutong.contact.service.ResidentCarService;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 住户车辆控制器
 *
 * @Author: liukunlong
 * @Date: 21/5/20 上午9:43
 */
@MvcAction
@ReqMapping("/admin/crm/resident/car")

public class ResidentCarController {

    @Inject
    private ResidentCarService residentCarService;

    @Inject
    private JdbcSession jdbcSession;

    /**
     * 根据条件获取住户汽车列表
     *
     * @param jsonObject 查询对象
     * @return 住户汽车列表
     * @throws IOException 操作异常
     */
    @ReqMapping("/page")
    public Page<ResidentCar> page(JSONObject jsonObject) throws Exception {
        return residentCarService.page(jsonObject);
    }

    @ReqMapping("/list")
    public Object list(JSONObject jsonObject) {
        return residentCarService.list(jsonObject);
    }

    @ReqMapping("/get")
    public Object get(ResidentCar residentCar) {
        return residentCar;
    }


    @Bean(newInstance = true, copy = true)
    @ReqMapping("/save")
    public Object save(ResidentCar residentCar) throws Exception {
        return residentCarService.saveOrUpdateResidentCar(residentCar);
    }

    @Bean(copy = true)
    @ReqMapping("/update")
    public Object update(ResidentCar residentCar) throws Exception {
        return residentCarService.saveOrUpdateResidentCar(residentCar);
    }

    @ReqMapping("/delete")
    public Object delete(ResidentCar residentCar) throws Exception {
        return residentCarService.delete(residentCar);
    }

    @ReqMapping("/getResidentCar")
    public Object getResidentCar(JSONObject jsonObject) throws Exception {
        ResidentCar residentCar= jdbcSession.findOne(ResidentCar.class)
                .eq(jsonObject, "id","carNo")
                .eq("deleted",false).exe();
        if(Objects.nonNull(residentCar)){
            residentCarService.buildResidentCarOtherData(residentCar);
        }
        return residentCar;
    }
}
